use crate::{error::AppErr, global::APP};

use super::{address::{name_with_address, HEAT}, proto::{address::{CARGO1, CARGO2, DROP}, Frame}};

mod drop;
mod cargo;
mod heat;

pub fn handle_notify(frame: Frame) {
    let src = frame.src();
    let ret = match src {
        CARGO1 | CARGO2 => cargo::exec(frame),
        DROP => drop::exec(frame),
        HEAT => heat::exec(frame),
        _ => Err(AppErr::Dyn(format!("unknown src: {}", src))),
    };

    if let Err(e) = ret {
        log::error!("notify err: {}", e);
    } 
}

pub fn handle_log(frame: Frame) {
    let buf: &[u8] = match frame.parse_raw() {
        Ok(buf) => buf,
        Err(e) => {
            log::error!("log parse err:{}", e);
            return;
        }
    };
    let msg = match String::from_utf8(buf.to_vec()) {
        Ok(msg) => msg,
        Err(e) => {
            log::error!("log string err:{}", e);
            return;
        }
    };
    let name = name_with_address(frame.src());
    
    let s = format!("{}:{}", name, msg);
  //  log::info!("log:{}", s);
    APP.notify_log(&s);
}
